package com.colin.project.controller;

import com.colin.project.bean.Video;
import com.colin.project.jedis.UserJedisOperate;
import com.colin.project.service.FileService;
import com.colin.project.service.UserService;
import com.colin.project.util.EncipherUtil;
import com.colin.project.util.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;
    @Autowired
    private UserJedisOperate userJedisOperate;
    @Autowired
    private EncipherUtil encipherUtil;
    @Autowired
    private ResponseResult<String> responseResult;

    /**
     * 视频上传
     * @param multipartFile
     * @param md5
     * @param session
     * @return
     */
    @RequestMapping("/videoUpload")
    @ResponseBody
    public ResponseResult<String> videoUpload(@RequestParam("video") MultipartFile multipartFile,
                                                @RequestParam("md5") String md5,
                                                HttpSession session) {
        return userService.videoUpload(multipartFile,session,md5);
    }


    /**
     * 查找视频地址
     * @param id
     * @param title
     * @param model
     * @return
     */
    @RequestMapping("/videoPlay")
    public String videoPlay(Integer id,
                            String title,
                            Model model) {
        String videopath = fileService.findVideo(id);
        model.addAttribute("videourl",videopath);
        model.addAttribute("title",title);
        return "videoPlay";
    }

    /**
     * 获取主页视频
     * @param model
     * @return
     */
    @RequestMapping("/getHomeVideos")
    public String findVideoRecords(Model model){
        List<Video> videoRecord = fileService.findVideoRecord();
        model.addAttribute("videoRecord", videoRecord);

        return "home";

    }

}
